package cn.lijiguo.concurrency.part1.threadobjectmethods;

/**
 * Wait
 * 展示 wait 和 notify 的基本用法
 * 1 研究代码的执行顺序
 * 2 证明 wait 释放锁
 * @author lijiguo
 * @date 2020/1/6 15:54
 */
public class Wait {

    public static Object object = new Object();

    public static void main(String[] args) throws InterruptedException {
        new Thread1("t1").start();
        Thread.sleep(100);
        new Thread2("t2").start();
    }

    static class Thread1 extends Thread {

        public Thread1(String name) {
            super(name);
        }

        @Override
        public void run() {
            synchronized (object) {
                System.out.println(Thread.currentThread().getName() + " 线程开始执行了。。。");

                try {
                    object.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                System.out.println(Thread.currentThread().getName() + " 线程获得到了锁~~~");
            }
        }
    }

    static class Thread2 extends Thread {

        public Thread2(String name) {
            super(name);
        }

        @Override
        public void run() {
            synchronized (object) {
                object.notify();
                System.out.println(Thread.currentThread().getName() + " 线程调用了 notify()...");
            }
        }
    }


}
